package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.*;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmaelDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王磊
 * @date 2022/5/10
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmaelDishService setmaelDishService;

    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> setmealPageDto = new Page<>(page, pageSize);
        //判断有没有传过来名字,传过来了就进行模糊查询
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name != null, Setmeal::getName, name);
        //查询出所有的套餐,根据套餐的分类id查询分类表并设置分类名称
        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);
        List<SetmealDto> dtoList = list.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Category category = categoryService.getById(item.getCategoryId());
            if (category!=null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealPageDto.setRecords(dtoList);
        return R.success(setmealPageDto);
    }


    @DeleteMapping
    public R<String> delete(Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        setmealService.removeByIds(list);
        return R.success("删除成功");
    }


    @PostMapping("/status/0")
    public R<String> update(Long[] ids) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(0);
        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getId, id);
            setmealService.update(setmeal, queryWrapper);
        }
        return R.success("修改成功");
    }


    @PostMapping("/status/1")
    public R<String> updates(Long[] ids) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(1);
        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getId, id);
            setmealService.update(setmeal, queryWrapper);
        }
        return R.success("修改成功");
    }

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }


    @GetMapping("{id}")
    public R<SetmealDto> update(@PathVariable Long id) {
        //获取一个套餐对象,里面保存着套餐的基本信息
        Setmeal setmeal = setmealService.getById(id);
        //根据套餐id.获取到一个套餐菜品关系表,里面都是菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> list = setmaelDishService.list(queryWrapper);
        //创建一个套菜菜品关系对象,里面包含套菜信息和菜品信息
        SetmealDto setmealDto = new SetmealDto();
        setmealDto.setSetmealDishes(list);
        BeanUtils.copyProperties(setmeal, setmealDto);
        return R.success(setmealDto);

    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getId, setmealDto.getId());
        setmealService.remove(lambdaQueryWrapper);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmaelDishService.remove(queryWrapper);

        setmealService.save(setmealDto);
        List<SetmealDish> list = setmealDto.getSetmealDishes();

        list = list.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());


        setmaelDishService.saveBatch(list);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        lambdaQueryWrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);
        return R.success(list);
    }

}
